/*
jQuery上下翻页滚动
*/
(function($){
	$.fn.extend({
	        Scroll:function(opt,callback){
	                //参数初始化
	                if(!opt) var opt={};
	                var _btnUp = $("#"+ opt.up);//向上按钮
	                var _btnDown = $("#"+ opt.down);//向下按钮
	                var timerID;
	                var heightHiff=opt.heightHiff?opt.heightHiff:0;
	                var _this=this.eq(0).find("ul:first");
	                var     lineH=opt.lineH?opt.lineH:_this.find("li:first").height() //获取行高
	                        line=opt.line?parseInt(opt.line,10):parseInt(this.height()/lineH,10),
	                        speed=opt.speed?parseInt(opt.speed,10):500,//卷动速度，数值越大，速度越慢（毫秒）
	                        timer=opt.timer //滚动的时间间隔（毫秒）
	                if(line==0) line=1;
	                var upHeight=0-line*lineH-heightHiff;
	               
	                //向上翻页函数
	                var scrollUp=function(){
	                       
	                        var conH=_this.parent().height();    
	                        var mainH= _this.height();                
	                        
	                        var marginHeight=Math.abs(parseInt(_this.css('marginTop')));	                        
	                        var topHeight=upHeight-marginHeight;
	                        if((-topHeight-(mainH-conH))>=0&&(-topHeight-(mainH-conH))<=-upHeight){
	                        	topHeight=-mainH+conH;
	                        }
	                        if(mainH<conH||-topHeight>(mainH-conH-upHeight))  return;
	                        _btnUp.unbind("click",scrollUp); //取消向上按钮的函数绑定	                        	                        
	                        _this.animate({
	                                marginTop:topHeight
	                        },speed,function(){
	                        	_btnUp.bind("click",scrollUp); //绑定向上按钮的点击事件
	                        });
	                        

	                }
	                //向下翻页函数
	                var scrollDown=function(){
	                        var conH=_this.parent().height();    
	                        var mainH= _this.height();    
	                        var marginHeight=parseInt(_this.css('marginTop').replace("px",""));
	                       
	                        var topHeight=marginHeight-upHeight;
	                        
	                        //if(mainH<conH||marginHeight==0)  return;  
	                        if(marginHeight>upHeight){
	                        	topHeight=0;
	                        }

	                        _btnDown.unbind("click",scrollDown);
	                        	                       
	                        _this.animate({
	                                marginTop:topHeight
	                        },speed,function(){
	                        	_btnDown.bind("click",scrollDown);
	                        });
	                        
	                }
	                
	               //自动播放
	                var autoPlay = function(){
	                        if(timer)timerID = window.setInterval(scrollUp,timer);
	                };
	                var autoStop = function(){
	                        if(timer)window.clearInterval(timerID);
	                };
	                 //鼠标事件绑定
	                _this.hover(autoStop,autoPlay).mouseout();
	                
	                //向上向下鼠标事件绑定
	                 _btnUp.css("cursor","pointer").click( scrollUp ).hover(autoStop,autoPlay);

	                _btnDown.css("cursor","pointer").click( scrollDown ).hover(autoStop,autoPlay);

	        }       
	})
})(jQuery);
